<template>
  <div>
    <!-- 弹出框3 -->
    <vol-box
      :lazy="true"
      :mask="true"
      v-model="model.box3"
      :title="model.xh"
      :height="800"
      :width="1500"
      :padding="15"
    >
      <div>
        
        <el-container>
          <el-main>
             <VolHeader style="margin: 30px 0" text="业务流程图">
</VolHeader>
            <el-steps ref="elstep" :active="act" finish-status="success">
              <el-step
                :title="item.title"
                
                v-for="item in stepdata"
              ></el-step>
            </el-steps>
          </el-main>
        </el-container>
        <reimfloat ref="reimfloat1"></reimfloat>
      </div>
      <div v-show="feiy">
        <view_reim_form ref="view_reim_form"></view_reim_form>
      </div>
       <div v-show="cnf">
        <view_reim_form_cc ref="view_reim_form_cc"></view_reim_form_cc>
      </div>
    <VolHeader style="margin: 30px 0" text="选择汇签人">
      </VolHeader>
      <el-row :gutter="20">
        <el-col :span="6">
          
          <div
        class="first"
        >
        <div class="header">
            <div class="text">公司组织机构</div>
          </div>
          <el-scrollbar height="380px" style="width: 100%">
            <el-tree
              ref="deptree"
              :data="dep_data"
              node-key="id"
              @node-expand="handleNodeClick"
              @node-click="click_handleNodeClick"
            />
          </el-scrollbar>
          </div>
        </el-col>
        <el-col :span="9">
           <div
        class="first"
        >
          <el-table :data="fromtableData" border height="420px">
            <el-table-column prop="username" v-if="false" label="真实姓名">
            </el-table-column>
            <el-table-column prop="usertruename" label="科室人员">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click.prevent="
                    transferRow(scope.$index, fromtableData, scope)
                  "
                >
                  选择 =>
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          </div>
        </el-col>
        <el-col :span="9">
           <div
        class="first"
        >
          <el-table :data="toData_gd" border height="180">
            <el-table-column prop="username" v-if="false" label="真实姓名">
            </el-table-column>
            <el-table-column prop="usertruename" label="后续审批人">
            </el-table-column>
            <el-table-column fixed="right" label="操作" v-if="false">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click.prevent="deleteRow(scope.$index, toData, scope)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          </div>
          <div
        class="sec"
        >
          <el-table :data="toData" border height="180">
            <el-table-column prop="username" v-if="false" label="真实姓名">
            </el-table-column>
            <el-table-column prop="usertruename" label="汇签人">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
              <template #default="scope">
                <el-button
                  type="text"
                  size="small"
                  @click.prevent="deleteRow(scope.$index, toData, scope)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          </div>
        </el-col>
        
      </el-row>
      
      <reimfloat_form ref="reimfloat_form1"></reimfloat_form>
      <template #footer>
        <div>
          <el-button type="primary" size="mini" @click="up_tijiao()"
            >提交</el-button
          >
          <!-- <el-button type="primary" size="mini" @click="up_tijiao('back')"
            >驳回</el-button
          > -->

          <el-button type="primary" size="mini" @click="closew()"
            >关闭</el-button
          >
        </div>
      </template>
    </vol-box>
  </div>
</template>
<script>
//import reimfloat from "@/extension/mytable/mytable/other/reimfloat_table.vue";

var ls_active = 0;
import reimfloat from "@/views/mytable/mytable/Fk_reimfloat.vue";
import VolBox from "@/components/basic/VolBox.vue";
import reimfloat_form from "@/extension/mytable/mytable/other/reimfloat_form.vue";
import http from "@/../src/api/http.js";
import view_reim_form from "@/extension/mytable/mytable/other/view_reim_form.vue";
import view_reim_form_cc from "@/extension/mytable/mytable/other/view_reim_form_cc.vue";
import VolHeader from '@/components/basic/VolHeader.vue';
export default {
  components: {
    "vol-box": VolBox,
    reimfloat,
    reimfloat_form,
    view_reim_form,
    view_reim_form_cc,
    VolHeader
  },
  data() {
    return {
      act: 0,
      feiy: false,
      cnf:false,
      stepdata: [],
      fromtableData: [],
      toData: [],
      toData_gd:[],
      origin_toData: [], //原始流程处理人，为不让前天删除
      dep_data: [],
      model: {
        //box2: false,
        box3: false,
        box3Row: {},
        xh: "",
      },
      tickno_rows: "", //检查是否重复填写大于"0"重复
    };
  },
  methods: {
    deleteRow(index, rows, scope) {
      //console.log(this.origin_toData)//从服务器端传来的固定接受人
      /* if (this.origin_toData.some((s) => s.username == scope.row.username)&&rows.length<=1) {
        //console.log(scope.row.username);
        this.$message.error("流程节点固定处理人至少需要一个人！");
        return;
      } */
      rows.splice(index, 1);
    },
    transferRow(index, rows, scope) {
      rows.splice(index, 1);
      // console.log(scope.row);
      this.toData.push(scope.row);
    },
    get_dep_user_add_table(puser) {
      //加载pdepartid部门下的员工
      this.fromtableData.splice(0, this.fromtableData.length);
      this.$nextTick(() => {
        if (puser.length > 0) {
          for (var i = 0; i <= puser.length - 1; i++) {
            this.fromtableData.push({
              username: puser[i].userName,
              usertruename: puser[i].userTrueName,
            });
          }
        }
      });
    },
    get_dep_fortree(note_type) {
      //加载公司名称
      this.dep_data.splice(0, this.dep_data.length); //清空数组
      this.$nextTick(() => {
        this.$nextTick(() => {
          var turl = "/api/Fk_reim/get_oayi_dep_frotree?note_type=" + note_type;
          this.http.post(turl, {}, true).then((result) => {
            if (note_type == -1) {
              //只加载公司名称
              //console.log(result.subcomp)
              for (let i = 0; i <= result.subcomp.length - 1; i++) {
                let obj = {
                  label: result.subcomp[i].subcompanyname,
                  subcompanyid: result.subcomp[i].company_oa_id,
                  note_type: -1, //-1本节点为根节点，公司级别
                  children: [{}],
                };
                this.dep_data.push(obj); //vue数组赋值push才能更新到界面上=huangxs2021年12月7日 14:44:52
              }
            }
            this.get_dep_user_add_table(result.puser);
          });
        });
      });
    },
    get_yj_dep_fortree(subcompanyid, data, node) {
      //获取一级部门，如信息部、质保、发展部
      const children = node.data.children || node.data;
      this.$nextTick(() => {
        this.$nextTick(() => {
          var turl =
            "/api/Fk_reim/get_oayi_dep_frotree?note_type=0&subcompanyid=" +
            subcompanyid;
          this.http.post(turl, {}, true).then((result) => {
            //console.log(result.yi_depart)
            for (let i = 0; i <= result.yi_depart.length - 1; i++) {
              if (result.yi_depart[i].depname_oa != "") {
                let obj = {
                  label: result.yi_depart[i].depname_oa,
                  depid_oa: result.yi_depart[i].depid_oa,
                  note_type: 0, //0本节点为一级部门节点
                  children: [{}],
                };

                data.children.push(obj); //vue数组赋值push才能更新到界面上=huangxs2021年12月7日 14:44:52
              }
            }

            this.get_dep_user_add_table(result.puser);
          });
        });
      });
    },
    get_gen_dep_fortree(pdepid, data, node) {
      //获取部门下面科室
      const children = node.data.children || node.data;
      this.$nextTick(() => {
        this.$nextTick(() => {
          var turl =
            "/api/Fk_reim/get_oayi_dep_frotree?note_type=1&pdepid=" + pdepid;
          this.http.post(turl, {}, true).then((result) => {
            for (let i = 0; i <= result.gen_depart.length - 1; i++) {
              if (result.gen_depart[i].depname_oa != "") {
                var obj;
                if (result.gen_depart[i].child_count == 0) {
                  obj = {
                    label: result.gen_depart[i].depname_oa,
                    depid_oa: result.gen_depart[i].depid_oa,
                    note_type: 0, //1本节点为普通部门节点
                  };
                } else {
                  obj = {
                    label: result.gen_depart[i].depname_oa,
                    depid_oa: result.gen_depart[i].depid_oa,
                    note_type: 0, //1本节点为普通部门节点
                    children: [{}],
                  };
                }

                data.children.push(obj); //vue数组赋值push才能更新到界面上=huangxs2021年12月7日 14:44:52
              }
            }
            //console.log(data)	   ;
            this.get_dep_user_add_table(result.puser);
          });
        });
      });
    },

    get_reimfloat_tu(reim_id) {
      //给流程图加载数据和固定接受人
      this.$nextTick(() => {
        var turl = "/api/Fk_reim/get_reimfloat_data?reim_id=" + reim_id;
        this.http.post(turl, {}, true).then((result) => {
          //console.log(result);
          this.stepdata.splice(0, this.stepdata.length);
          this.act = 0;
          //var obj;此处为坑，var定义要放在for巡皇里面，否则前台界面更新不完整huangxs2021年12月10日 21:43:17
          for (let ii = 0; ii <= result.fk_reimfloat.length - 1; ii++) {
            //console.log(result.fk_reimfloat[ii].stepid+"[]")
            //console.log(this.model.box3Row[0].stepid)
            if (
              result.fk_reimfloat[ii].stepid == this.model.box3Row[0].stepid
            ) {
              //this.act=i;//表示当前步骤号
              ls_active = ii;
              //console.log(ls_active+"dd");
            }

            let obj = {
              title: result.fk_reimfloat[ii].stepname,
              description: result.fk_reimfloat[ii].bdatetime,
              //index:result.fk_reimfloat[i].stepid,
            };
            this.stepdata.push(obj);
          }
          this.act = ls_active;
          //加载提交人员数据
          this.toData.splice(0, this.toData.length);
          this.toData_gd.splice(0, this.toData_gd.length);
          for (let ri = 0; ri <= result.next_ren.length - 1; ri++) {
            this.toData_gd.push({
              username: result.next_ren[ri].userName,
              usertruename: result.next_ren[ri].userTrueName,
            });
            this.origin_toData.push({
              username: result.next_ren[ri].userName,
              usertruename: result.next_ren[ri].userTrueName,
            });
          }
          //this.origin_toData=this.toData;不能这样赋值，因为origin_toData与toData都是同样的地址数据，等于是一个数据；
        });
      });
    },
    click_handleNodeClick(data, node) {
      //
      var turl = "/api/Fk_reim/get_renfordep?pdepid=" + data.depid_oa;
      this.http.post(turl, {}, true).then((result) => {
        console.log(result.puser);
        this.fromtableData.splice(0, this.fromtableData.length);
        this.$nextTick(() => {
          if (result.puser.length > 0) {
            for (var i = 0; i <= result.puser.length - 1; i++) {
              this.fromtableData.push({
                username: result.puser[i].userName,
                usertruename: result.puser[i].userTrueName,
              });
            }
          }
        });
      });
    },
    handleNodeClick(data, node) {
      //
      const children = node.data.children;
      children.splice(0, children.length); //先清空所有的子节点
      if (data.note_type == -1) {
        //当前点击的为公司节点，下面需要加载一级部门
        this.get_yj_dep_fortree(data.subcompanyid, data, node);
      }
      if (data.note_type == 0) {
        //当前点击的为部门，下面需要加载子部门
        this.get_gen_dep_fortree(data.depid_oa, data, node);
      }

      //node-key="id" id为每个节点唯一值，是控件自己分配的
      //const parent = node

      /* const children = node.data.children || node.data
			//console.log(children)
		    const index = children.findIndex((d) => d.id === data.id)
		    children.splice(index, 1)//删除node下的子控件
			*/
      /* const newChild = { depid: '676', label: 'testtest', children: [{}] }
		          if (!data.children) {
		            data.children = []
		          }
		          data.children.push(newChild)
		    //this.dataSource = [...this.dataSource] */
    },
    up_tijiao() {

       let ls_type="pre";
       let yj=this.$refs.reimfloat_form1.$refs.myform.formFields.reyijian;
       if (yj=="同意"){
         ls_type="pre";
       }else{
         ls_type="back";//驳回
       }
       //if 
      /* if (this.toData.length <= 0) {
        this.$message.error("没有选择下一步骤接受人！");
        return;
      } */

      var reim_id = this.model.box3Row[0].reim_id;
      var stepid = this.model.box3Row[0].stepid;
      let _reimlist = this.$store.getters.data()._alldata.extra.reimlist;
      let _rf_sqj = this.$store.getters.data()._alldata.extra.rf_sqj; //财务是否审批解除预算限制
      let _ys_query = this.$store.getters.data()._alldata.extra.ys_query; //预算
      let cys = 0;
     // console.log(_rf_sqj);
      for (let ii = 0; ii <= _reimlist.length - 1; ii++) {
        //只有for才能break
        let row = _reimlist[ii];
        if (row.reim_id == reim_id) {
          //console.log(row);
          let _tt = _ys_query.filter((x) => {
            //console.log(x.kyed);
            //console.log(row.je);
            return (
              x.racct == row.fei_type &&
              x.rcntr == row.rcntr &&
              row.je <= x.kyed &&
              row.je > 0
            ); //满足这些条件才允许提交,预算允许范围
            //return x.racct == "6602020101" && x.rcntr == "9000050001"
          });
          //console.log(_tt);
          if (_tt.length > 0) {
            //满足提交
            cys = 0;
          } else {
            cys = 1; //超预算或者预算为空没填
            break; //当有多条行项目，其中有一条不满足就break
          }
        }
      }
      /* _reimlist.forEach(row => {
				
				}); */
    
      if (
        _rf_sqj.some((s) => s.reimid == reim_id  && s.node_type == "sqj_ok") ==
        true
      ) {
  
        cys = 0; //直接通过
      }
      
      //console.log(cys)
      if (cys == 1) {
        this.$message.error("不允许提交！请检查科目、成本中心、是否超预算。");
        return;
      }
      if (this.tickno_rows >= "1") {
        //>1表示票号有重复,同样的票号有一个以上，排除了自己只有>=1说明之前重复的票号
        this.$message.error("票号重复或空，不允许提交，请检查！");
        return;
      }
      //console.log(this.$store.getters.data().keyed);
      //return;

      /* if (stepid == 9) {
        //原来是9999
        return this.$message.error("当前流程已经结算，不能提交！");
      } */

      //console.log(this.$refs.reimfloat_form1.$refs.myform.formFields)
      //console.log(this.toData);
      //return;
      var postdata = this.$refs.reimfloat_form1.$refs.myform.formFields;
      var pdata = encodeURIComponent(JSON.stringify(this.toData)); //webapi接受多个对象处理方式
      //console.log(postdata);
      //return;
      var turl =
        "/api/Fk_reim/up_tijiao?reimid=" +
        reim_id +
        "&current_stepid=" +
        stepid +
        "&tp=" +
        ls_type +
        "&pdata=" +
        pdata;
      this.http.post(turl, postdata, true).then((result) => {
        //console.log(result);
        //  this.$parent.$parent.$parent.$parent.$parent.$parent.$refs.reimfloat1.$refs.grid.refresh();//找到上一层的兄弟控件刷新，查询新数据
        if (!result.status) {
          return this.$message.error(result.message);
        } else {
          this.model.box3 = false;
          this.$emit("parentCall", ($vue) => {
            //大哥你这写的什么代码，把刷新放到异步请求的外面，我也是服了2021年12月2日 22:32:46
            $vue.search(); //search()为ViewGrid控件的search()
          }); //刷新父控件查询
          this.$Message.info(result.message);
        }
      });
    },
    closew() {
      this.model.box3 = false;
    },
    open3(row) {
      this.model.box3Row = row;
      var ls_remark = "";
      var ls_fk_reimfloat_id = "";
      this.model.xh = "当前报销序号：" + row[0].reim_id;

      this.$nextTick(() => {
        //等子组件refs.reimfloat_form1，reimfloat1的DOM全部加载后，执行下面的语句，但是对于子组件的事件控制不了。
        this.$nextTick(() => {
          this.$refs.reimfloat1.$refs.grid.refresh();
         /*  this.$refs.reimfloat_form1.$refs.myform.reset({
            remark: "",
            fk_reimfloat_id: "",
          }); */
        });
      });
      http
        .post("/api/Fk_reim/get_curr_reimfloat?reimid=" + row[0].reim_id, {})
        .then((rtn) => {
          //console.log(rtn);
          let result = rtn.reimfloat;
          this.tickno_rows = rtn.tickno_rows;
          this.$nextTick(() => {
            //等子组件refs.reimfloat_form1，reimfloat1的DOM全部加载后，执行下面的语句，但是对于子组件的事件控制不了。

            if (result.remark == undefined || result.remark == null) {
              ls_remark = " "; //带空格，否则前台不刷新
              //ls_remark="";
            } else {
              ls_remark = result.remark;
            }

            if (
              result.fk_reimfloat_id == undefined ||
              result.fk_reimfloat_id == null
            ) {
              ls_fk_reimfloat_id = "";
            } else {
              ls_fk_reimfloat_id = result.fk_reimfloat_id;
            }
            //console.log(result.remark);
            //给子组件赋值
            /* this.$refs.reimfloat_form1.$refs.myform.reset({
              //remark: ls_remark,//不显示审理意见,会把协同意见带处理
              fk_reimfloat_id: ls_fk_reimfloat_id,
            }); */
            //this.$refs.reimfloat_form1.$refs.myform.formFields["remark"]=ls_remark;
          });
        });
      this.get_dep_fortree(-1); //-1加载根目录  

      this.get_reimfloat_tu(row[0].reim_id);
      this.model.box3 = true;
      //steps控件初始化界面不能根据参数更新界面，应该是steps控件的BUG，只能用两个setTimeout暂时能满足要求huangxs-2021年12月11日 11:04:59
    setTimeout(() => {
        this.$nextTick(() => {
          this.act = 1; //这步骤把起始编号设为1才有效（0不是完全有效果），应该是steps控件的bug,以后注意要等界面上所有的控件都完全显示出来再改变控件的状态
          //console.log(this.act+"==")
        });
      }, 500);
      setTimeout(() => {
        this.$nextTick(() => {
          this.act = ls_active;
          //console.log(ls_active)
        });
      }, 1000); 

       this.$nextTick(() => {
         
        //console.log(this.$store.getters.data()._alldata.extra.reimlist);//.$refs.grid.rowData
        if (row[0].typeid == 1) {
          this.feiy = true;
          let params = {
            page: 1, //分页页数(可不填)
            rows: 30, //分页大小(可不填)
            sort: "排序字段", //可不填
            order: "desc/asc", //可不填
            wheres: [{ name: "reim_id", value: row[0].reim_id }], // 查询条件(可不填)
          };
          this.$store.getters.data().curr_reimid=row[0].reim_id
          this.$refs.view_reim_form.$refs.tableList.load(params, true); //查询审核页的业务数据
          for (var item in this.$refs.view_reim_form.$refs.myform.formFields) {
           
            this.$refs.view_reim_form.$refs.myform.formFields[item] =
              row[0][item]; //给界面赋值
           
          }
        }else
        {
          this.cnf=true;
           let params = {
            page: 1, //分页页数(可不填)
            rows: 30, //分页大小(可不填)
            sort: "排序字段", //可不填
            order: "desc/asc", //可不填
            wheres: [{ name: "reim_id", value: row[0].reim_id }], // 查询条件(可不填)
          };
          this.$store.getters.data().curr_reimid=row[0].reim_id
          this.$refs.view_reim_form_cc.$refs.tableList.load(params, true); //查询审核页的业务数据
          for (var item in this.$refs.view_reim_form_cc.$refs.myform.formFields) {
             console.log(item+"=======================================================")
            console.log(row[0][item]);
            this.$refs.view_reim_form_cc.$refs.myform.formFields[item] =
              row[0][item]; //给界面赋值
            //console.log(item)
          }
        }
          
        
      }); 
    },
  },
};
</script>
<style>
.el-row {
  margin-bottom: 20px;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.header {
  display: flex;
  margin-bottom: 10px;
}

.text {
  line-height: 30px;
  border-bottom: 2px solid #607d8b;
  font-size: 16px;
  /* color: #FF9800; */
  font-weight: 500;
}
.first {
  padding-top: 1px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
.sec {
  padding-top: 55px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
</style>
